﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls.WebParts;
using Castle.Windsor;
using M2B.GestionEntretiens.Contracts;
using M2B.GestionEntretiens.Helpers.JSON;
using M2B.GestionEntretiens.Models;
using M2B.GestionEntretiens.POCOS;
using Newtonsoft.Json;

namespace M2B.GestionEntretiens.Controllers
{
    public class QuestionsController : Controller
    {
        #region Properties
        /// <summary>
        /// Type des questions
        /// </summary>
        public List<Referentiel> TypeQuestions
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel!=null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "TYPE_QUESTION").ToList();
                }
                return new List<Referentiel>();
            }
        }

        /// <summary>
        /// Evaluation des questions
        /// </summary>
        public List<Referentiel> EvaluationQuestions
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "EVALUATION_QUESTION").ToList();
                }
                return new List<Referentiel>();
            }
        }

        /// <summary>
        ///Nb Reponses Question
        /// </summary>
        public List<Referentiel> NbReponsesQuestion
        {
            get
            {
                var refrentielsModel = Session["RefrentielsModel"] as RefrentielsModel;
                if (refrentielsModel != null)
                {
                    return refrentielsModel.Referentiels.Where(el => el.MotCle == "NB_REPONSES_QUESTIONS").ToList();
                }
                return new List<Referentiel>();
            }
        } 

        public QuestionsModel QuestionsModel
        {
            get
            {

                var questionsModel = Session["QuestionsModel"] as QuestionsModel;
                if (questionsModel == null)
                {
                    var container = Session["Container"] as IWindsorContainer;
                    if (container != null) questionsModel = new QuestionsModel(container.Resolve<IQuestionsService>());
                    Session["QuestionsModel"] = questionsModel;
                }
                return questionsModel;

            }
            private set { Session["QuestionsModel"] = value; }
        }
        #endregion

        //
        // GET: /Questions/

        public ActionResult Index()
        {
            return View();
        }
        
        //
        // GET: /Questions/Edit/5

        public ActionResult EditQuestion(int questionId)
        {

            ViewData["TypeQuestions"] = TypeQuestions;
            ViewData["EvaluationQuestions"] = EvaluationQuestions;
            ViewData["NbReponsesQuestion"] = NbReponsesQuestion;

            Question question = QuestionsModel.GetQuestion(questionId);
            return PartialView(question);
        }

        public ActionResult AddQuestion()
        {
            ViewData["TypeQuestions"] = TypeQuestions;
            ViewData["EvaluationQuestions"] = EvaluationQuestions;
            ViewData["NbReponsesQuestion"] = NbReponsesQuestion;

            return PartialView(new Question(){Type = "1",Evaluation = "1",MaxReponses = 1});
        }

        [HttpPost]
        public ActionResult CreateQuestion(Question question)
        {
            try
            {
                QuestionsModel.CreateQuestion(question);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        //
        // POST: /Questions/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            try
            {
                QuestionsModel.Update(question);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        //
        // Post: /Questions/Delete/5
        [HttpPost]
        public ActionResult DeleteQuestion(int questionId)
        {
            try
            {
                QuestionsModel.DeleteQuestion(questionId);
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Index");
            }
        }

        /// <summary>
        /// Chargement de la liste des questions
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadQuestions()
        {
            string strQuestions = JsonConvert.SerializeObject(QuestionsModel.Questions,
              new EFNavigationPropertyConverter());
            return Json(strQuestions, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Chargement de la liste des propositions d'une question à modifier
        /// </summary>
        /// <param name="questionId"></param>
        /// <returns></returns>
        public ActionResult LoadPropositions(int questionId)
        {
            var question = QuestionsModel.Questions.FirstOrDefault(el => el.QuestionId == questionId);

            if (question != null)
            {
                string strPropositions = JsonConvert.SerializeObject(question.Propositions.Select(prop => new Proposition() { PropositionId = prop.PropositionId, ContenuProposition = prop.ContenuProposition, IsReponse = prop.IsReponse, }),
              new EFNavigationPropertyConverter());
                return Json(strPropositions, JsonRequestBehavior.AllowGet);
            }

            return Json(new List<Proposition>(), JsonRequestBehavior.AllowGet);
        }
    }
}
